package com.ycw.controller.wjk;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycw.bean.Admin;
import com.ycw.bean.Comments;
import com.ycw.bean.Funding;
import com.ycw.bean.Projects;
import com.ycw.bean.Users;
import com.ycw.service.wjk.WeiService;
import com.ycw.util.Msg;

@Controller
public class WeiController {

	@Autowired
	WeiService weiService;
	
	
	//判断用户邮箱
	@RequestMapping("/checkUsers")
	@ResponseBody
	public Msg checkUsers(String usEmail) {
		//System.out.println("usEmail=  "+usEmail);
		boolean f=weiService.checkUsers(usEmail);
		if (f) {
			return Msg.success();
		}else {
			return Msg.fail();
		}
	}
	
	//用户登录
	@RequestMapping("/usersLogin")
	public String usersLogin(Users users,HttpSession session) {
		//System.out.println(users.getUsEmail()+users.getUsPassword());
		if (users.getUsEmail() == "" || users.getUsPassword() == "") {
			return "/login.jsp";
		}else {
			users=weiService.usersLogin(users);
			if (users!=null) {
				session.setAttribute("users", users);
				System.out.println(users.getUsId()+"  "+users.getUsRole());
				return "redirect:index";
			} else {
				return "/login.jsp";
			}
			
		}
		
	}
	
	//管理员登录
	@RequestMapping("/adminLogin")
	public String adminLogin(Admin admin,HttpSession session) {
		System.out.println(admin.getAdminName()+admin.getAdminPassword());
		if (admin.getAdminName() == "" || admin.getAdminPassword() == "") {
			return "/login1.jsp";
		}else {
			admin=weiService.adminLogin(admin);
			if (admin==null) {
				return "/login1.jsp";
			}else {
				session.setAttribute("admin", admin);
				System.out.println(admin.getAdminId());
				return "back/index";
			}
		}
		
			
	}
	
	
	//查看所有项目
	@RequestMapping("/getAllProjects")
	@ResponseBody
	public Msg getAllProjects(@RequestParam(value="pn",defaultValue="1")Integer pn) {
		PageHelper.startPage(pn,6);
		List<Projects> list=weiService.getAllProjects();
		PageInfo page=new PageInfo(list);
		
		return Msg.success().add("pageInfo",page);
	}
	
	//用户首页根据项目名模糊查询项目
	@RequestMapping("/getProjectsByPsName")
	@ResponseBody
	public Msg getProjectsByPsName(String psName,@RequestParam(value="pn",defaultValue="1")Integer pn) {
		PageHelper.startPage(pn,5);
		List<Projects> list=weiService.getProjectsByPsName(psName);
		PageInfo page=new PageInfo(list);
		return Msg.success().add("pageInfo",page);
	}
	
	
	
	//详情页
	@RequestMapping("/getProjectDetaile")
	@ResponseBody
	public Msg getProjectDetaile(Integer psId,@RequestParam(value="pn",defaultValue="1")Integer pn) {
		System.out.println("psId=  "+psId);
		Map<String, Object> map=weiService.getProjectDetaile(psId,pn);
		return Msg.success().add("map", map);
	}
	
	
	//参与众筹
	@RequestMapping("/addFunding")
	@ResponseBody
	public Msg addFunding(Funding funding,HttpSession session) {
		
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		funding.setFdTime(simpleDateFormat.format(new Date().getTime()));
		System.out.println(funding.getFdPsId()+funding.getFdUsId()+funding.getFdTime());
		boolean f=weiService.addFunding(funding);
		if (f) {
			//今日投资金额增加，今日投资人数增加
			if (session.getServletContext().getAttribute("todayMoney")==null||session.getServletContext().getAttribute("todaynumber")==null) {
				session.getServletContext().setAttribute("todayMoney", funding.getFdMoney());
				session.getServletContext().setAttribute("todaynumber", 1);
			}else {
				System.out.println(session.getServletContext().getAttribute("todayMoney"));
				System.out.println(funding.getFdMoney());
				session.getServletContext().setAttribute("todayMoney", (Double)session.getServletContext().getAttribute("todayMoney")+funding.getFdMoney());
				session.getServletContext().setAttribute("todaynumber", (Integer)session.getServletContext().getAttribute("todaynumber")+1);
			}
			//System.out.println(session.getServletContext().getAttribute("todayMoney"));
			//System.out.println(session.getServletContext().getAttribute("todaynumber"));
			return Msg.success();
				
			}else {
				return Msg.fail();
			}
		}
	
	
	//获取今日投资金额和投资人数
	@RequestMapping("/getNumberAndMoney")
	public Msg getNumberAndMoney(HttpSession session) {
		if (session.getServletContext().getAttribute("todayMoney")==null||session.getServletContext().getAttribute("todaynumber")==null) {
			//System.out.println("111");
			session.getServletContext().setAttribute("todayMoney", 0.0);
			session.getServletContext().setAttribute("todaynumber", 0);
		}else {
			session.getServletContext().setAttribute("todayMoney", (Double)session.getServletContext().getAttribute("todayMoney"));
			session.getServletContext().setAttribute("todaynumber", (Integer)session.getServletContext().getAttribute("todaynumber"));
			System.out.println(session.getServletContext().getAttribute("todayMoney"));
			System.out.println(session.getServletContext().getAttribute("todaynumber"));
		}
		//System.out.println(session.getServletContext().getAttribute("todayMoney"));
		return Msg.success();
	}
	
	
	//定时任务每日清除金额和人数
	@Scheduled(cron = "0 0 0 * * ?")
	public Msg timedTasks() {
		//sc相当于session.getServletContext()
		ServletContext sc = ContextLoader.getCurrentWebApplicationContext().getServletContext();
		sc.setAttribute("todayMoney", 0.0);
		sc.setAttribute("todaynumber", 0);
		System.out.println("todayMoney  ");
		return null;
	}
	
	
	
	//发起评论
	@RequestMapping("/addComments")
	@ResponseBody
	public Msg addComments(Comments comments) {
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		comments.setCmTime(simpleDateFormat.format(new Date().getTime()));
		boolean f=weiService.addComments(comments);
		if (f) {
			return Msg.success();
		}else {
			return Msg.fail();
		}
	}
	
	
	//查询未激活的用户
	@RequestMapping("/getUsersByUsRole")
	@ResponseBody
	public Msg getUsersByUsRole(@RequestParam(value="pn",defaultValue="1")Integer pn) {
		PageHelper.startPage(pn,5);
		Users users=new Users();
		users.setUsRole(0);
		List<Users> list=weiService.getUsersByUsRole(users);
		PageInfo page=new PageInfo(list);
		return Msg.success().add("pageInfo", page);
	}
	
	//激活用户，审核是否填写身份证，手机号
	@RequestMapping("/updateUserRole")
	@ResponseBody
	public Msg updateUserRole(Users users) {
		users.setUsRole(1);
		boolean f=weiService.updateUserRole(users);
		if (f) {
			return Msg.success();
		}else {
			return Msg.fail();
		}
	}
	
	
	//余额充值,页面将session中getUsMoney的值加上充值的值放到setUsMoney中
		@RequestMapping("/addUsersAcount")
		@ResponseBody
		public Msg addUsersAcount(Users users,double account) {
			if (account<0) {
				return Msg.fail();
			}else {
				users.setUsMoney(users.getUsMoney()+account);
				boolean f=weiService.addUsersAcount(users);
				if (f) {
					return Msg.success();
				}else {
					return Msg.fail();
				}
				
			}
			
		}
		
		//查看自己投资明细
		@RequestMapping("/getOneUsersMoneyDetail")
		@ResponseBody
		public Msg getOneUsersMoneyDetail(Users users) {
			Funding funding=new Funding();
			funding.setFdUsId(users.getUsId());
			List<Funding> list=weiService.getOneUsersMoneyDetail(funding);
			return Msg.success().add("list", list);
		}
		
}
